﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Approval;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Approval
{
    public class AppUpdataCommendHandlers : IRequestHandler<AppUpdataCommends, APIResult<LeaveApplications>>
    {

        private readonly IBaseRepository<LeaveApplications> laRepository;
        private readonly IBaseRepository<Record> recordRepository;
        private readonly IBaseRepository<User> userRepository;
        private readonly IMapper mapper;
        public AppUpdataCommendHandlers(IBaseRepository<LeaveApplications> laRepository, IBaseRepository<Record> recordRepository, IBaseRepository<User> userRepository, IMapper mapper)
        {
            this.laRepository = laRepository;
            this.recordRepository = recordRepository;
            this.userRepository = userRepository;
            this.mapper = mapper;
        }



        /// <summary>
        /// 更新状态以及添加审批记录
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<LeaveApplications>> Handle(AppUpdataCommends request, CancellationToken cancellationToken)
        {
            APIResult<LeaveApplications> result = new APIResult<LeaveApplications>();
            result.Code = APIEnum.添加成功;
            result.Msg= "添加成功";
            var leaveApplications = laRepository.GetAllAsync().Result.FirstOrDefault(p => p.ApplicationId == request.ApplicationId);
            if (leaveApplications == null)
            {
                throw new Exception("未找到申请表");
            }

            DateTime Now = DateTime.Now;
            #region 添加记录信息
            Record record = new Record();
            record.ApplicationId=leaveApplications.ApplicationId;
            record.DepId=leaveApplications.DepId;
            record.Reason=leaveApplications.Reason;
            record.ApplicationName = leaveApplications.UserName;
            record.ApprovalOpinion = request.ApprovalOpinion;
            record.ApproveStatus = request.ApproveStatus; //0:通过 1:拒绝
            record.ApprovalName = request.UserName;
            record.ApprovalCreatetime = Now;
            record.Reasonrejection = request.Reasonrejection;
            await recordRepository.Add(record);

            #endregion

            #region 更新状态

       
            //通过
            leaveApplications.Status = 1;//处理中
         
            leaveApplications.DepheadsName = request.UserName;

            leaveApplications.CreateTime = Now;
            var users=userRepository.GetAllAsync().Result.FirstOrDefault(p=>p.Heads==2).UserName;
            //如果当前登录用户是总经理，则审核完成
          if (leaveApplications.DepheadsName == users)
            {
                leaveApplications.Status = 2;//审核已完成
            }

            //拒绝
            if (request.ApproveStatus == 1)
            {
                leaveApplications.Status = 3;//审核驳回
            }
          leaveApplications.Reasonrejection = request.Reasonrejection;
            await laRepository.Update(leaveApplications);

            #endregion

            return await Task.FromResult(result);
        }
    }
}
